package com.shameit.goodqrinfomanager.ui.activity.cangku;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProviders;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.chad.library.BR;
import com.shameit.goodqrinfomanager.AppViewModelFactory;
import com.shameit.goodqrinfomanager.BaseMVVMActivity;
import com.shameit.goodqrinfomanager.R;
import com.shameit.goodqrinfomanager.databinding.ActivityCangkuManagementBinding;
import com.shameit.goodqrinfomanager.ui.activity.barcode.BarCodeManagementActivity;
import com.shameit.goodqrinfomanager.ui.activity.barcode.BarCodeViewModel;

public class CangkuManagementActivity extends BaseMVVMActivity<ActivityCangkuManagementBinding,CangkuManagementViewModel> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolBarBack(binding.toolbar);
        //setContentView(R.layout.activity_cangku_management);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_cangku_management;
    }

    @Override
    public int initVariableId() {
        return BR.data;
    }

    @Override
    public CangkuManagementViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(CangkuManagementViewModel.class);
    }

    @Override
    protected String setTitle() {
        return "仓库管理";
    }

    @Override
    protected void initView() {
        binding.spCwbh.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                viewModel.cwbh.set(binding.spCwbh.getSelectedItem().toString());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        binding.cangkuSpCklx.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                viewModel.czlx.set(binding.cangkuSpCklx.getSelectedItem().toString());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @Override
    protected void loadData() {
        String[] arr = viewModel.loadCangkuItem();
        ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item, arr);
        binding.spCkbh.setAdapter(arrayAdapter);

        binding.spCkbh.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                binding.spCwbh.setAdapter(null);
                //binding.spCpbh.setAdapter(null);
                String thisCangku = arr[position];
                binding.spCwbh.setAdapter(new ArrayAdapter(CangkuManagementActivity.this, R.layout.support_simple_spinner_dropdown_item, viewModel.loadCangweiItem(thisCangku)));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        if (arr.length>0){
            String thisCangkuNo = binding.spCkbh.getSelectedItem().toString();
            arrayAdapter = new ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item, viewModel.loadCangweiItem(thisCangkuNo));
            binding.spCwbh.setAdapter(arrayAdapter);

            binding.spCwbh.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    viewModel.cwbh.set(binding.spCwbh.getSelectedItem().toString());
//                    String[] cpcode = viewModel.loadChanpinItem();
//                    ArrayAdapter chanpinAdapter = new ArrayAdapter(BarCodeManagementActivity.this, R.layout.support_simple_spinner_dropdown_item, cpcode);
//                    binding.spCpbh.setAdapter(chanpinAdapter);
                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }
            });
        }
    }
}